package com.projeto.business.controller;



import javax.ejb.Stateless;
import javax.persistence.Query;

import com.projeto.infra.business.GenericBean;
import com.projeto.persistence.GroupTable;
import com.projeto.persistence.User;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;


@Stateless
public class UserController extends GenericBean<User>{

        public UserController(){
            super();
        }
	
	/* (non-Javadoc)
	 * @see com.novo.projeto.business.IUserBean#find(java.lang.String)
	 */
	public User find(String id) {
		System.out.println("Find User By ID:" + id);
		return em.find(User.class, id);
	}
	
	/* (non-Javadoc)
	 * @see com.novo.projeto.business.IUserBean#findAll()
	 */
	@SuppressWarnings("unchecked")
	public List<User> findAll() {
		Query query = em.createQuery("SELECT e FROM User e");
		return (List<User>) query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<User> findAllActive() {
		Query query = em.createQuery("SELECT e FROM User e");
		return (List<User>) query.getResultList();
	}
        
	public List<GroupTable> findAllGrouptTables() {
		Query query = em.createQuery("SELECT e FROM GroupTable e");
		return (List<GroupTable>) query.getResultList();
	}
        
        public List<User> findByName(String name) {
            Query query = em.createQuery("SELECT e FROM User e WHERE e.name LIKE '" + name + "%'");
            return (List<User>) query.getResultList();
        }
        
        @TransactionAttribute(TransactionAttributeType.REQUIRED)
        public void save(User user)
        {
            if (user == null)
            {
                throw new IllegalArgumentException("User cannot be null");
            }
            
            User temp = em.find(User.class, user.getLogin());
            if (temp == null)
            {
                temp = user;
            }
            else
            {
                temp.setName(user.getName());
                temp.setPassword(user.getPassword());
                temp.setGroupTables(user.getGroupTables());
            }
            temp.setUpdateDt(new Date());
            
            System.out.println(user.toString());
            Set<GroupTable> groupTables = new HashSet<GroupTable>();
            if (temp.getGroupTables() != null && !temp.getGroupTables().isEmpty())
            {
                for(Object gt: temp.getGroupTables())
                {
                    String groupId = gt.toString();
                    System.out.println("GROUPID: " + groupId);
                    
                    GroupTable g = em.find(GroupTable.class, groupId);

                    groupTables.add(g);
                }
            }
            temp.setGroupTables(groupTables);
            em.persist(temp);
            System.out.println(user.toString());

        }

    @Override
    protected boolean validate(User e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

	
}
